package hyskidotmateuszatgmail.recepcja.fragments;

import hyskidotmateuszatgmail.recepcja.LazyAdapter;
import hyskidotmateuszatgmail.recepcja.R;
import hyskidotmateuszatgmail.recepcja.adapter.KlasaStatyczna;
import interfejsy.IWsp;
import interfejsy.logika.Operacje;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import listener.DD;
import listener.secondListener;
import model.Lokalizacje;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FragmentLokalizacje extends Fragment {
    
    public static View rootView;
      ListView list;  
      LazyAdapter adapter;
      private String[] amStrings={"a","b"};
      public static void update(Map<String,Object> map)
  	{
    	  if( map.containsKey("P"))
    	  {
    	  String h=null;
    	  for (Map.Entry<String, Object> entry : map.entrySet()) {
    			System.out.println("Key : " + entry.getKey() + " Value : "
    				+ entry.getValue());
    			if ( entry.getKey()=="C")
    				h+=entry.getKey();
    		}
    	  Button bb = (Button) rootView.findViewById(R.id.pop2);
    	  bb.setText(h);
    	  }
    	  if(map.containsKey("lista"))
    	  {
    		  itemsList.add("pppp");
    		  //adapter1.notifyDataSetChanged();
				itemsList.add("z");
				//edit.setText("");
				//a.notifyDataSetChanged();
				//adapter1.notifyDataSetChanged();
    	  }
  	}
      static String[] listViewItems1 = new String[] {"Android", "iOS"};
      private static ListView listview;
      private static String[] listViewItems = new String[] {"Android", "iOS", "Windows Phone", "Blackberry", "Meego", "Symbian"};
      private static List<String> itemsList = new ArrayList<String> ( ) ; 
  	private static ArrayAdapter<String> adapter1;
  	void filllist()
  	{
  	
  		itemsList.clear();
  		for(Lokalizacje l:model.KlasaStatyczna.lokalizacje)
  			itemsList.add(l.getNazwa());	
  	}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
    	KlasaStatyczna.przekaz("",0);

    	filllist();
    	//KlasaStatyczna.setAc(getActivity());
           /* KlasaStatyczna.setFra(this);
            KlasaStatyczna.setContext(getActivity().getApplicationContext());*/
    	//itemsList.add ( "Item 1" ); 
    	//itemsList.add ( "Item 2" ) ;
    	KlasaStatyczna.act=getActivity();
    	KlasaStatyczna.fragmenty.put("Lokalizacje",this);
    	
            rootView = inflater.inflate(R.layout.fragmentlokalizacje, container, false);
                listview = (ListView) rootView.findViewById(R.id.listView1);
                /*DD s=new DD();
                s.set(itemsList);
                listview.setOnItemClickListener(s);*/
                
                /*listview.setAdapter(new ArrayAdapter<String>(KlasaStatyczna.act,
		                android.R.layout.simple_list_item_1, itemsList));*/
                a=new StableArrayAdapter(KlasaStatyczna.act,
		                android.R.layout.simple_list_item_1, itemsList);
                /*
                adapter1 = new ArrayAdapter<String>(getActivity()KlasaStatyczna.act,
		                android.R.layout.simple_list_item_1, itemsList);*/
                //listview.setAdapter(a);
                //listview.setAdapter(adapter1);
                
                //////////////
                //adapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemsList);
                
                /** Defining a click event listener for the button "Add" */
                OnClickListener listener = new OnClickListener() {			
        			@Override
        			public void onClick(View v) {								
        				EditText edit = (EditText) rootView.findViewById(R.id.editText1);
        				ProgressDialog ringProgressDialog= ProgressDialog.show(
        	                    KlasaStatyczna.getContext(),
        	                    "Recepcja",     "Aktualizacja listy lokalizacji...", true);
        				ringProgressDialog.setCancelable(false);
        				KlasaStatyczna.przekaz(edit.getText().toString(),1);
        				filllist();
        				System.out.println("SIZE "+KlasaStatyczna.lokalizacje.size());
        				ringProgressDialog.dismiss();
        				a.notifyDataSetChanged();
        				//adapter1.notifyDataSetChanged();
        			}
        		};
        		Button bb = (Button) rootView.findViewById(R.id.pop2);
        		//bb.setOnClickListener(new secondListener());
        		
                //bb.setOnClickListener(listener/*new secondListener()*/);
                bb.setOnClickListener(listener/*new secondListener()*/);
                listview.setAdapter(a);   
                
            
            return rootView;
    }
    private static StableArrayAdapter a;
    private class StableArrayAdapter extends ArrayAdapter<String> {
        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
        public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
          }
        }
        @Override
        public long getItemId(int position) {
          String item = getItem(position);
          return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
          return true;
        }

      }
}
